'use strict'
var AWS = require('aws-sdk');

function setClient() {
    this.efs_client = new AWS.S3({
        endpoint: this.end_point,
        accessKeyId: this.accessKey_id,
        secretAccessKey: this.secret_access_key,
        sessionToken: this.session_token,
        s3ForcePathStyle: true
    })
}

class EfsClient {

    constructor(efs_config) {
        if (!efs_config) throw error('efs config is required')
        this.end_point = efs_config.efsAddress,
        this.accessKey_id = efs_config.credentials.accessKeyId,
        this.secret_access_key = efs_config.credentials.secretAccessKey,
        this.session_token = efs_config.credentials.sessionToken
        setClient.call(this)
    }

    putObject(bucket, key, body, ext) {

        let contentType = "video/" + ext
        let params = {
            Bucket: bucket,
            Key: key,
            Body: body,
            ContentType: contentType
        }
        let instance = this
        return new Promise((reslove, reject) => {

            instance.efs_client.putObject(params, (err, data) => {
                if (err) 
                    return reject(err)
                else
                    return reslove(data)
            })
        })

    }
    
}

module.exports = EfsClient